package com.hyxt.DTO.process;

import com.hyxt.DTO.CommonMessage;
import com.hyxt.DTO.CommonMessageHeader;
import com.hyxt.DTO.T809_MessageHeader;
import com.hyxt.superviseservice.CommonMessageBody;
import com.hyxt.superviseservice.IMessageBody;
import com.hyxt.superviseservice.messageBody.T809_0x1202;
import com.hyxt.utils.BytesUtil;
import com.hyxt.utils.T809_Process;
import com.hyxt.utils.T809_Util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @Project: CNPC_VMS
 * @Title: T809_0x1202_Process
 * @Description:
 * @author: denghl
 * @date: 2014年4月24日 下午5:11:36
 * @company: Beijing Huayou Information andCommunication Technology Co.,Ltd
 * @Copyright: Copyright (c) 2014
 * @version V2.0
 */
public class T809_0x1202_Process<B extends IMessageBody> extends
		T809_Process<B> {
	private static Log log = LogFactory.getLog(T809_0x1202_Process.class);

	@Override
	protected CommonMessageBody getBody(CommonMessageHeader header, byte[] data) {
		// TODO Auto-generated method stub
		T809_0x1202 body = new T809_0x1202();
		// 车牌号
		byte[] VEHICLE_NO = BytesUtil.cutBytes(0, 21, data);
		// 车牌颜色
		byte VEHICLE_COLOR = BytesUtil.getByte(21, data);
		// 数据类型
		byte[] DATA_TYPE = BytesUtil.getWord(22, data);
		// 数据长度
		byte[] DATA_LENGTH = BytesUtil.cutBytes(24, 4, data);
		// 数据加密标识
		byte excrypt = BytesUtil.getByte(28, data);
		// 年月日
		byte[] date = BytesUtil.cutBytes(29, 4, data);
		// 时间
		byte[] time = BytesUtil.cutBytes(33, 3, data);
		// 经度
		byte[] lon = BytesUtil.cutBytes(36, 4, data);
		// 纬度
		byte[] lat = BytesUtil.cutBytes(40, 4, data);
		// 速度
		byte[] vec1 = BytesUtil.getWord(44, data);
		// 行驶记录速度
		byte[] vec2 = BytesUtil.getWord(46, data);
		// 车辆当前里程数
		byte[] vec3 = BytesUtil.cutBytes(48, 4, data);
		// 方向
		byte[] direction = BytesUtil.getWord(52, data);
		// 海拔高度
		byte[] altitude = BytesUtil.getWord(54, data);
		// 车辆状态
		byte[] state = BytesUtil.cutBytes(56, 4, data);
		// 报警状态
		byte[] alarm = BytesUtil.cutBytes(60, 4, data);

		try {
			body.setVEHICLE_NO(new String(VEHICLE_NO, "gbk"));
		} catch (UnsupportedEncodingException e) {
			log.warn("", e);
		}
		// 设置属性
		body.setVEHICLE_COLOR(VEHICLE_COLOR);
		body.setDATA_TYPE(BytesUtil.parseBytesToShort(DATA_TYPE));
		body.setDATA_LENGTH(BytesUtil.parseBytesToInt(DATA_LENGTH));
		body.setExcrypt(excrypt);
		body.setDate(date);
		// 设置时间
		body.setTime(time);
		body.setLon(BytesUtil.parseBytesToInt(lon));
		body.setLat(BytesUtil.parseBytesToInt(lat));
		body.setVec1(BytesUtil.parseBytesToShort(vec1));
		body.setVec2(BytesUtil.parseBytesToShort(vec2));
		// 设置里程数
		body.setVec3(BytesUtil.parseBytesToShort(vec3));
		body.setDirection(BytesUtil.parseBytesToShort(direction));
		body.setAltitude(BytesUtil.parseBytesToShort(altitude));
		body.setState(BytesUtil.parseBytesToInt(state));
		body.setAlarm(BytesUtil.parseBytesToInt(alarm));
		return body;
	}

	/**
	 * @Description:数据打包类
	 */
	@Override
	protected byte[] packData(CommonMessage<B> message) {
		// TODO Auto-generated method stub
		T809_0x1202 body1202 = (T809_0x1202) message.getMessageBody();
		byte[] head = new byte[22];
		// head_flag(1)+head(22)+body(64)+CRC(2)+end_flag(1)=90
		byte[] datalength = BytesUtil.int2bytes4(90);
		datalength = BytesUtil.getBigDWord(0, datalength);
		System.arraycopy(datalength, 0, head, 0, datalength.length);

		// 报文序列号*占4字节
		byte[] MSG_SN = BytesUtil.int2bytes4(((T809_MessageHeader) message
				.getMessageHeader()).getSn());
		head[4] = MSG_SN[3];
		head[5] = MSG_SN[2];
		head[6] = MSG_SN[1];
		head[7] = MSG_SN[0];
		// 业务数据类型,占2字节
		// byte[] MSG_ID =
		// BytesUtil.int2bytes2(Integer.parseInt((((T809_MessageHeader)message.getMessageHeader()).getMessageID()).toString()));
		head[8] = (byte) 0x12;
		head[9] = 0x00;
		// 下级平台接入码，上级平台给下级平台分配唯一标识码。占4字节
		byte[] MSG_GNSSCENTERID = BytesUtil
				.int2bytes4(((T809_MessageHeader) message.getMessageHeader())
						.getGnsscentrid());
		MSG_GNSSCENTERID = BytesUtil.getBigDWord(0, MSG_GNSSCENTERID);
		System.arraycopy(MSG_GNSSCENTERID, 0, head, 10, MSG_GNSSCENTERID.length);
		// 协议版本好标识，上下级平台之间采用的标准协议版编号；长度为3个字节来表示，0x01 0x02 0x0F
		// 标识的版本号是v1.2.15，以此类推。
		byte[] VERSION_FLAG = ((T809_MessageHeader) message.getMessageHeader())
				.getVersonFlag();
		System.arraycopy(VERSION_FLAG, 0, head, 14, VERSION_FLAG.length);
		// 报文加密标识位b: 0表示报文不加密，1表示报文加密。占1字节
		byte ENCRYPT_FLAG = ((T809_MessageHeader) message.getMessageHeader())
				.getEncryptFlag();

		head[17] = ENCRYPT_FLAG;
		// 数据加密的密匙，长度为4个字节。
		byte[] ENCRYPT_KEY = BytesUtil.int2bytes4(((T809_MessageHeader) message
				.getMessageHeader()).getEncryptKey());
		
		ENCRYPT_KEY = BytesUtil.getBigDWord(0, ENCRYPT_KEY);
		
		System.arraycopy(ENCRYPT_KEY, 0, head, 18, 4);

		byte[] body = new byte[64];

		// 车牌号
		byte[] vehicle_No = null;
		try {
			vehicle_No = body1202.getVEHICLE_NO().getBytes("GBK");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		System.arraycopy(vehicle_No, 0, body, 0, vehicle_No.length);

		byte vehicle_Color = body1202.getVEHICLE_COLOR();

		body[21] = vehicle_Color;

		// byte[] data_Type = BytesUtil.int2bytes2(body9205.getDATA_TYPE());
		body[22] = (byte) 0x12;
		body[23] = 0x02;
		// System.arraycopy(data_Type, 0, body, 22, data_Type.length);

		byte[] data_Length = BytesUtil.int2bytes4(36);
		data_Length = BytesUtil.getBigDWord(0, data_Length);
		System.arraycopy(data_Length, 0, body, 24, data_Length.length);
		// 对数据头到校验码前的CRC值,占两个字节
		byte excrypt = body1202.getExcrypt();
		body[28] = excrypt;

		byte[] date = body1202.getDate();
		System.arraycopy(date, 0, body, 29, date.length);
		// 截取时间
		byte[] time = body1202.getTime();
		System.arraycopy(time, 0, body, 33, time.length);

		byte[] lon = BytesUtil.intToByteArray(body1202.getLon());
		System.arraycopy(lon, 0, body, 36, lon.length);
		// 截取纬度
		byte[] lat = BytesUtil.intToByteArray(body1202.getLat());
		System.arraycopy(lat, 0, body, 40, lat.length);

		byte[] vec1 = BytesUtil.int2bytes2(body1202.getVec1());

		System.arraycopy(vec1, 0, body, 44, vec1.length);
		// 截取行驶记录速度
		byte[] vec2 = BytesUtil.int2bytes2(body1202.getVec2());
		System.arraycopy(vec2, 0, body, 46, vec2.length);

		byte[] vec3 = BytesUtil.intToByteArray(body1202.getVec3());
		System.arraycopy(vec3, 0, body, 48, vec3.length);
		// 截取方向
		byte[] direction = BytesUtil.int2bytes2(body1202.getDirection());
		System.arraycopy(direction, 0, body, 52, direction.length);

		byte[] altitude = BytesUtil.int2bytes2(body1202.getAltitude());
		System.arraycopy(altitude, 0, body, 54, altitude.length);
		// 截取车辆状态
		byte[] state = BytesUtil.intToByteArray(body1202.getState());
		System.arraycopy(state, 0, body, 56, state.length);
		// 截取报警状态
		byte[] alarm = BytesUtil.intToByteArray(body1202.getAlarm());
		System.arraycopy(alarm, 0, body, 60, alarm.length);

//		body = T809_Util.encrypt(2768156, body, 0, 0, 30000000);

//		datalength = BytesUtil.int2bytes4(body.length + 26);
//		datalength = BytesUtil.getBigDWord(0, datalength);
//		System.arraycopy(datalength, 0, head, 0, datalength.length);

		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] mes = null;
		try {
			bos.write(0x00);
			// 将数据头与数据体写入流中
			bos.write(head);
			bos.write(body);
			bos.write(0x00);
			bos.write(0x00);
			bos.write(0x00);
			// 将数据流转成byte数组
			byte[] bosBytes = bos.toByteArray();
			// 计算crc校验码
			byte[] CRCCode = T809_Util.check(bosBytes);
			bosBytes[bosBytes.length - 3] = CRCCode[0];
			bosBytes[bosBytes.length - 2] = CRCCode[1];
			// 将数据转义
			mes = T809_Util.escapeData(bosBytes);
			mes[0] = 0x5b;
			// 加入数据头与数据尾
			mes[mes.length - 1] = 0x5d;
		} catch (IOException e) {
			log.warn("", e);
		} finally {
			// 如果数据流不为空，关闭流
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					log.warn("", e);
				}
			}
		}

		// log.debug("实时上传车辆定位信息消息" + BytesUtil.bytesToHexString(mes));
		return mes;
	}

	public static void main(String[] args) {

//		T809_0x1202_Process<T809_0x1202> process = new T809_0x1202_Process<T809_0x1202>();
//
//		byte[] data = null;
//
//		try {
//			data = T809_Util.reverseEscapeData(data);
//			T809_MessageHeader header = T809_Util.getHeader(data);
//			T809_0x1202 body = (T809_0x1202) process.getBody(header, data);
//
//			log.debug(body);
//
//		} catch (ProtocolEscapeExeption e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		byte[] datas = BytesUtil.toStringHex("5b0000005a02027471e51200009896830100000000000000bcbd4a4a33373031000000000000000000000000000212020000002400080a07e00a233406f398a0025a0197f20000000000000000007c000000000002000000003e455d");
		
		BytesUtil.cutBytes(0, 50, datas);
		try {
			log.debug(new String(datas,"GBK"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
